/*
 * Copyright 2011 Helios Technologies.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.heliostech.gwt.widget.dialog.error;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * @author ldesdier
 *
 */
public class ErrorDetail extends Composite {

	public interface Resources extends ClientBundle {		
		@Source(Style.DEFAULT_CSS)
		@CssResource.NotStrict
	    Style errorDetailStyle();
	}
	
	@ImportedWithPrefix("helios-ErrorDetail")
	public interface Style extends CssResource {
		String DEFAULT_CSS = "com/heliostech/gwt/widget/dialog/error/ErrorDetail.css";
		
		String errorDetailWidget();
		String errorLabel();
		String error();
		String causeLabel();
		String cause();
		String detailLabel();
		String detail();
	}
	
	private static Resources DEFAULT_RESOURCES;
	private static Resources getDefaultResources() {
		if (DEFAULT_RESOURCES == null) {
			DEFAULT_RESOURCES = GWT.create(Resources.class);
		}
		return DEFAULT_RESOURCES;
	}	
	private final Style style;
	
	private final Label error;
	private final Label cause;
	private final Label detail;
	
	/**
	 * 
	 */
	public ErrorDetail() {
		this(getDefaultResources());
	}
	
	public ErrorDetail(String e, String c, String d) {
		this(e, c, d, getDefaultResources());
	}
	
	public ErrorDetail(Resources res) {
		DEFAULT_RESOURCES = res;
		this.style = res.errorDetailStyle();
		this.style.ensureInjected();
		final FlowPanel main = new FlowPanel();
		
		final Label errorLabel = new Label("Error");
		errorLabel.addStyleName(style.errorLabel());
		error = new Label();
		error.addStyleName(style.error());
		
		final Label causeLabel = new Label("Caused by");
		causeLabel.addStyleName(style.causeLabel());
		cause = new Label();
		cause.addStyleName(style.cause());
		
		final Label detailLabel = new Label("Details");
		detailLabel.addStyleName(style.detailLabel());
		detail = new Label();
		detail.addStyleName(style.detail());
		
		main.add(errorLabel);
		main.add(error);
		main.add(causeLabel);
		main.add(cause);
		main.add(detailLabel);
		main.add(detail);
		
		initWidget(main);
		addStyleName(style.errorDetailWidget());
	}
	
	public ErrorDetail(String e, String c, String d, Resources res) {
		this(res);
		setError(e);
		setCause(c);
		setDetail(d);
	}
	
	public void setError(String e) {
		error.setText(e);
	}
	
	public void setCause(String c) {
		cause.setText(c);
	}
	
	public void setDetail(String d) {
		detail.setText(d);
	}

}
